package com.rich.service.impl;

import com.rich.constant.Constant;
import com.rich.dao.UserDao;
import com.rich.dao.impl.UserDaoImpl;
import com.rich.domain.User;
import com.rich.service.UserService;
import com.rich.utils.BeanFactory;
import com.rich.utils.MailByQQ;
import com.rich.utils.MailUtils;

public class UserServiceImpl implements UserService {

	UserDao ud = (UserDao) BeanFactory.getBean("UserDao");
	/**
	 * 用户注册
	 * @throws Exception 
	 */
	@Override
	public void regist(User user) throws Exception {
		//保存到数据库中
		ud.save(user);
		
		//发送激活邮件
		String emailMsg="恭喜"+user.getName()+":激活成功,<a href='http://localhost:8080/Store/user?method=active&code="+user.getCode()+"'>点击激活</a>";
		MailByQQ.sendEmail(user.getEmail(), emailMsg);
	}

	

	/**
	 * 用户激活
	 */
	
	@Override
	public User active(String code) throws Exception {
		//通过code获取用户
		User user = ud.getByCode(code);
		
		if(user == null) {
			//通过code未找到用户
			return null;
		}
		user.setState(Constant.USER_IS_ACTIVE);
		user.setCode(null);
		ud.update(user);
		return user;
	}


	/**
	 * 登陆
	 */
	@Override
	public User login(String userName, String password) throws Exception {
		User user = ud.getUser(userName,password);
		return user;
	}


}
